<?php

/**
 *      [Hero! Cms] (C)2011-2012.
 *      
 *      $Id: function_login.php 29 2011-08-17 12:15:02Z zyb0807@gmail.com $
 */

if(!defined('IN_HERO')) {
	exit('Access Denied');
}

/**
* 登陆函数
* @return 登录状态
* 1 = 成功， 2 = 成功自动激活，0 = 失败
*/
function userlogin($username, $password) {
	global $_G;
	$return = array();

	$member = DB::fetch_first("SELECT * FROM ".DB::table('common_member')." WHERE username='".$username."'");
	if(!$member) {
		$return['status'] = -1;
		return $return;
	}

	$md5password = md5(md5($password).$member['uid']);
	if($member['password'] !== $md5password) {
		$return['status'] = -2; 
		return $return;
	}

	$return['member'] = $member;
	$return['status'] = 1;
	return $return;
}

/**
 * 管理员登陆
 */
function adminlogin($uid, $password) {
	global $_G;
	$return = array();

	$member = DB::fetch_first("SELECT * FROM ".DB::table('common_member')." WHERE uid='".$uid."'");
	if(!$member) {
		$return['status'] = -1;
		return $return;
	}

	$md5password = md5(md5($password).$member['uid']);
	if($member['password'] !== $md5password) {
		$return['status'] = -2; 
		return $return;
	}

	$return['member'] = $member;
	$return['status'] = 1;
	return $return;
}


function setloginstatus($member, $cookietime) {
	global $_G;
	$_G['uid'] = $member['uid'];
	$_G['username'] = $member['username'];
	$_G['adminid'] = $member['adminid'];
	$_G['groupid'] = $member['groupid'];
	$_G['formhash'] = formhash();
	$_G['member'] = $member;
	$_G['core']->session->isnew = 1;

	//note 存放登录数据
	dsetcookie('auth', authcode("{$member['password']}\t{$member['uid']}", 'ENCODE'), $cookietime, 1, true);
	dsetcookie('loginuser');
	dsetcookie('activationauth');
}

?>

